from app.data.Subject import Subject
from app.data.Comment import Comment
from app.data.User import User
from app.dao.user_management import search_user
from app.dao.comment_management import add_comment,delete_comment,search_comment,search_comment_by_subjectID
from app.dao.subject_management import add_subject,delete_subject,search_subject

class CommentBackend:
    @staticmethod
    def comment_subject(userin:User,commentin:Comment):
        ret = 0
        if userin.userID is None:
            ret = 5001
        elif commentin.content is None:
            ret = 5002
        elif commentin.subjectID is None:
            ret = 5013
        else:
            user = search_user(userin, "userID")[0]
            subject = search_subject(Subject(subjectID= commentin.subjectID),"subjectID")[0]
            if user is None:
                ret = 5003
            elif subject is None:
                ret = 5006
            else:
                if add_comment(Comment(content=commentin.content,userID=commentin.userID,subjectID=commentin.subjectID,reply=0)) is True:
                    ret = 1
                else:
                    ret = 0
        return ret

    @staticmethod
    def comment_comment(userin:User,commentin:Comment):
        ret = 0
        if userin.userID is None:
            ret = 5001
        elif commentin.content is None:
            ret = 5002
        elif commentin.reply is None:
            ret = 5007
        elif commentin.subjectID is None:
            ret = 5013
        elif commentin.reply != 0:
            user = search_user(userin, "userID")[0]
            subject = search_subject(Subject(subjectID= commentin.subjectID),"subjectID")[0]
            comment = search_comment(Comment(commentID=commentin.reply))[0]
            if user is None:
                ret = 5003
            elif subject is None:
                ret = 5004
            elif comment is None:
                ret = 5008
            else:
                if add_comment(Comment(content=commentin.content,reply=commentin.reply,userID=commentin.userID,subjectID=commentin.subjectID)) is True:
                    ret = 1
                else:
                    ret = 0
        return ret

    @staticmethod
    def comment_little_comment(userin:User,commentin:Comment,at:int):
        ret = 0
        if userin.userID is None:
            ret = 5001
        elif commentin.content is None:
            ret = 5002
        elif commentin.subjectID is None:
            ret = 5013
        elif commentin.reply is None:
            ret = 5007
        elif at is None:
            ret = 5010
        elif commentin.reply != 0:
            user = search_user(userin, "userID")[0]
            subject = search_subject(Subject(subjectID= commentin.subjectID),"subjectID")[0]
            comment = search_comment(Comment(commentID=commentin.reply))[0]
            at_comment = search_comment(Comment(commentID=at))[0]
            if user is None:
                ret = 5003
            elif subject is None:
                ret = 5004
            elif comment is None:
                ret = 5008
            elif at_comment is None:
                ret = 5011
            else:
                content_with_at = "@"+at_comment.userID+" "+commentin.content
                if add_comment(Comment(content=content_with_at,reply=commentin.reply,userID=commentin.userID,subjectID=commentin.subjectID)) is True:
                    ret = 1
                else:
                    ret = 0
        return ret

    @staticmethod
    def comment_delete(userin:User,commentin:Comment):
        ret = 0
        if userin.userID is None:
            ret = 5001
        elif commentin.commentID is None:
            ret = 5012
        else:
            user = search_user(userin, "userID")[0]
            comment = search_comment(Comment(commentID=commentin.commentID),"commentID")[0]
            if user is None:
                ret = 5003
            elif user.type == "Teaacher" or user.type == "admin":
                if delete_comment(commentin,"commentID") is True:
                    ret = 1
                else:
                    ret = 0
            elif user.userID ==comment.userID:
                if delete_comment(commentin,"commentID") is True:
                    ret = 1
                else:
                    ret = 0
        return ret

    @staticmethod
    def big_comment_display(userin:User,subjectin:Subject):
        ret = 0
        if userin.userID is None:
            ret = 5001
        elif subjectin.subjectID is None:
            ret = 5013
        else:
            user = search_user(userin, "userID")[0]
            subject = search_subject(Subject(subjectID=subjectin.subjectID), "subjectID")[0]
            comment_list = search_comment_by_subjectID(Comment(subjectID=subjectin.subjectID,reply=0),"reply")
            if user is None:
                ret = 5003
            elif subject is None:
                ret = 5004
            elif len(comment_list)==0:
                ret = 5015
            else:
                ret = comment_list
        return ret

    @staticmethod
    def little_or_lilittle_comment_display(userin:User,commentin:Comment):
        ret = 0
        if userin.userID is None:
            ret = 5001
        elif commentin.subjectID is None:
            ret = 5013
        elif commentin.commentID is None:
            ret = 5017
        else:
            user = search_user(userin, "userID")[0]
            subject = search_subject(Subject(subjectID= commentin.subjectID),"subjectID")[0]
            comment = search_comment(Comment(commentID=commentin.reply))[0]
            comment_list = search_comment_by_subjectID(Comment(subjectID=commentin.subjectID,reply=commentin.commentID),"reply")
            if user is None:
                ret = 5003
            elif len(subject):
                ret = 5004
            elif len(comment):
                ret = 5008
            elif len(comment_list)==0:
                ret = 5016
            else:
                ret = commentin
        return ret

if __name__ == '__main__':
    ret = []
    print(ret[0])


